package de.tum.in.eist.poll.server.persistence;

import java.util.ArrayList;
import java.util.Date;

//import org.joda.time.DateTime;

import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.User;

public class StaticDatabase extends Database {

	private static ArrayList<User> users = new ArrayList<User>();
	private static ArrayList<Lecture> lectures = new ArrayList<Lecture>();

	private static StaticDatabase instance;

	private StaticDatabase() {

		User student = new User("student", "new");
		User student2 = new User("student2", "new");
		User lecturer = new User("seidl", "new", User.LECTURER);
		User lecturer2 = new User("prof", "new", User.LECTURER);
		users.add(student);
		users.add(student2);
		users.add(lecturer);
		users.add(lecturer2);

		Feedback feed1 = new Feedback(student, 1, "Yeey, Freibier fuer Alle!",
				"Das Projekt ist vielleicht ein Schrott -.-");
		Feedback feed2 = new Feedback(student2, 3, "Weltfrieden.",
				"Ich geh heim!");
		java.util.Date date2 = new java.util.Date();
		java.util.Date date1 = new java.util.Date();
		date1.setDate(date2.getDate() - 1);
		java.util.Date date3 = new java.util.Date();
		date3.setDate(date2.getDate() + 7);
		Lecture l = new Lecture("EIST PROJECT LECTURE 1", lecturer, date1);
		Lecture l2 = new Lecture("EIST PROJECT LECTURE 2", lecturer2, date2);
		Lecture l3 = new Lecture("EIST PROJECT LECTURE 2", lecturer2, date3);

		l.addFeedback(feed1);
		l2.addFeedback(feed2);

		lectures.add(l);
		lectures.add(l2);
		lectures.add(l3);
	}

	public static StaticDatabase getInstance() {
		if (instance == null)
			instance = new StaticDatabase();
		return instance;
	}

	public User validateSessionID(String sID) {
		//TODO
//		for (User u : users) {
//			if (u.getSessionID() != null)
//				if (u.getSessionID().equals(sID))
//					return u;
//		}
		return null;
	}

	@Override
	public User validateUser(String email, String psswd) {
		for (User u : users) {
			if (u.getEmail().equals(email) && u.getPassword().equals(psswd)) {
				return u;
			}
		}
		return null;
	}

	@Override
	public Lecture addNewLecture(User user, Lecture lecture) {
		if (user.getAccountLevel() < 1) {
			return null;
		}
		lectures.add(lecture);
		return lecture;
	}

	@Override
	public ArrayList<Lecture> addNewLectures(User user,
			ArrayList<Lecture> lectures) {
		if (user.getAccountLevel() < User.LECTURER) {
			return null;
		}
		for (Lecture l : lectures) {
			this.lectures.add(l);
		}
		return lectures;
	}

	@Override
	public Lecture addFeedbackToLecture(Feedback feedback, Lecture lecture) {
		if (userHasRatedLecture(feedback.getAuthor(), lecture)) {
			return changeFeedback(feedback, lecture);
		}
		for (Lecture l : lectures) {
			if (l.equals(lecture)) {
				l.addFeedback(feedback);
				addNotification(feedback.getAuthor(), l);
				return l;
			}
		}
		return null;
	}

	@Override
	public Lecture changeFeedback(Feedback feedback, Lecture lecture) {
		if (!userHasRatedLecture(feedback.getAuthor(), lecture)) {
			return addFeedbackToLecture(feedback, lecture);
		}
		for (Lecture l : lectures) {
			if (l.equals(lecture)) {
				ArrayList<Feedback> lFeedbacks = l.getFeedbacks();
				for (int i = 0; i < lFeedbacks.size(); i++) {
					if (lFeedbacks.get(i).getAuthor()
							.equals(feedback.getAuthor())) {
						lFeedbacks.remove(i);
						l.addFeedback(feedback);
						return l;
					}
				}
			}
		}
		return null;
	}

	@Override
	public ArrayList<Lecture> getAllLectures() {
		return lectures;
	}

//	@Override
//	protected ArrayList<Lecture> getLecturesFromTo(DateTime start, DateTime end) {
//		ArrayList<Lecture> result = new ArrayList<Lecture>();
//		for (Lecture l : lectures) {
//			DateTime lDate = Database.convertDateToDateTime(l.getDate());
//			if (start.compareTo(lDate) >= 0 && end.compareTo(lDate) <= 0) {
//				result.add(l);
//			}
//		}
//		return result;
//	}
	
	@Override
	public ArrayList<Lecture> getLecturesFromTo(Date start, Date end) {
		ArrayList<Lecture> result = new ArrayList<Lecture>();
		for (Lecture l : lectures) {
			Date lDate = l.getDate();
			if (start.compareTo(lDate) >= 0 && end.compareTo(lDate) <= 0) {
				result.add(l);
			}
		}
		return result;
	}

	@Override
	protected ArrayList<Lecture> findLecture(String searchTerm) {
		ArrayList<Lecture> result = new ArrayList<Lecture>();
		for (Lecture l : lectures) {
			if (l.getTopic().contains(searchTerm)) {
				result.add(l);
			}
		}
		return result;
	}

	@Override
	public boolean userHasRatedLecture(User user, Lecture lecture) {
		for (Lecture l : lectures) {
			if (l.equals(lecture) && l.userHasRatedLecture(user.getKey())) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected void addNotification(User user, Lecture lecture) {
		for(User u : users){
			if(!u.equals(user)){
				u.addNotification(lecture);
			}
		}
	}

	@Override
	public User clearAllNotifications(User user) {
		for(User u : users){
			if(u.equals(user)){
				u.clearAllNotifications();
				return u;
			}
		}
		return null;
	}

	@Override
	public User clearNotification(User user, Lecture lecture) {
		for(User u : users){
			if(u.equals(user)){
				u.clearNotification(lecture);
				return u;
			}
		}
		return null;
	}
}
